package com.aj.activity.adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aj.activity.R;
import com.aj.databases.entry.PrayLetterInfo;

public class PrayLetterAdapter extends BaseAdapter {

	private Context context;

	private List<PrayLetterInfo> prayLetterInfoList;

	private HashMap<Integer, View> viewCache = new HashMap<Integer, View>();

	public PrayLetterAdapter(Context context,
			List<PrayLetterInfo> prayLetterInfoList) {
		this.context = context;
		this.prayLetterInfoList = prayLetterInfoList;
	}

	@Override
	public int getCount() {
		return prayLetterInfoList.size();
	}

	@Override
	public Object getItem(int position) {
		return prayLetterInfoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		TextView username;
		TextView content_text;
		TextView time_text;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = viewCache.get(position);
		ViewHolder viewHodler = null;
		PrayLetterInfo prayLetterInfo = prayLetterInfoList.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_child_prayletter, null);
			viewHodler = new ViewHolder();
			viewHodler.username = (TextView) convertView
					.findViewById(R.id.username);
			viewHodler.content_text = (TextView) convertView
					.findViewById(R.id.content_text);
			viewHodler.time_text = (TextView) convertView
					.findViewById(R.id.time_text);
			convertView.setTag(viewHodler);
			viewCache.put(position, convertView);
		} else {
			viewHodler = (ViewHolder) convertView.getTag();
		}
		viewHodler.username.setText(prayLetterInfo.getCallName() == null ? ""
				: prayLetterInfo.getCallName());
		viewHodler.content_text.setText(prayLetterInfo.getContent());
		viewHodler.time_text.setText(prayLetterInfo.getCreateDate());
		return convertView;
	}
}
